/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker;

import java.util.ArrayList;
import sahd.broker.server.Service;

/**
 *
 * @author Romms
 */
public class RegisteredServiceRepository {

    private ArrayList <Service> services;

    public RegisteredServiceRepository(){
        services = new ArrayList<Service>();
    }

    public boolean serviceIsRegistered(int serviceCode){
        for(int serviceIndex=0;serviceIndex<services.size();serviceIndex++){
            if(services.get(serviceIndex).getServiceCode()==serviceCode)
                return true;
        }
        return false;

    }

    public boolean registerService(Service service){
        if(this.serviceIsRegistered(service.getServiceCode())){
            return false;
        }
        this.services.add(service);
        return true;
    }

    private Service foundService(Service service){
        for(int serviceIndex=0;serviceIndex<services.size();serviceIndex++){
            if(services.get(serviceIndex).getServiceCode()==service.getServiceCode())
                return services.get(serviceIndex);
        }
        return null;
    }

    private Service foundService(int serviceProtocol){
        for(int serviceIndex=0;serviceIndex<services.size();serviceIndex++){
            if(services.get(serviceIndex).getServiceCode()==serviceProtocol)
                return services.get(serviceIndex);
        }
        return null;
    }
    public Service getService(int serviceProtocol){
        return this.foundService(serviceProtocol);
    }
    

    public boolean unregisterService(Service service){
        if(this.serviceIsRegistered(service.getServiceCode())){
            services.remove(foundService(service));
            return true;
        }
        return false;
    }

}
